package com.dyz.wsw.front.market.controller;

import com.dyz.wsw.common.web.JsonResult;
import com.dyz.wsw.front.market.service.CardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * 处理卡片请求的控制器类
 * @author dyz
 * @date 2025/1/14 23:36
 */
@RestController
@RequestMapping("/card")
@Slf4j
public class CardController {


    private final CardService cardService;

    public CardController(CardService cardService){
        log.debug("创建控制器对象: CardController");
        this.cardService = cardService;
    }


    /**
     * 获取更多卡片
     * @param cid 卡片目录
     * @param skipSize 跳过条数
     * @param requestSize 请求条数
     * @return 卡片数据
     */
    @GetMapping("/more/{cid}/{skipSize}/{requestSize}")
    public Mono<JsonResult> getMoreCards(@PathVariable(required = false) Long cid,
                                         @PathVariable Integer skipSize,
                                         @PathVariable Integer requestSize){
        return cardService.getMoreCards(cid,skipSize,requestSize).collectList().map(JsonResult::ok);
    }

    @GetMapping("/more/{skipSize}/{requestSize}")
    public Mono<JsonResult> getMoreCards(@PathVariable Integer skipSize,
                                         @PathVariable Integer requestSize){
        return cardService.getMoreCards(null,skipSize,requestSize).collectList().map(JsonResult::ok);
    }


    /**
     * 请求获取指定数量的随机卡片
     * @param count 请求数量
     * @return 请求数量的随机卡片
     */
    @GetMapping("/rand/{count}")
    public Mono<JsonResult> getRandomCards(@PathVariable Integer count){
        return cardService.getRandCards(count)
                .collectList()
                .map(JsonResult::ok);
    }

    /**
     * 获取卡片总条数
     * @return 卡片总条数
     */
    @GetMapping("/total/")
    public Mono<JsonResult> getCardsTotal(){
        return cardService.getCardsCount(null).map(JsonResult::ok);
    }

    @GetMapping("/total/{cid}")
    public Mono<JsonResult> getCardsTotal(@PathVariable(required = false) Long cid){
        return cardService.getCardsCount(cid).map(JsonResult::ok);
    }


    /**
     * 获取指定页指定数量的卡片
     * @param cid 目录id
     * @param currentPage 当前页
     * @param requestCount 请求数量
     * @return 卡片数据
     */
    @GetMapping("/page/{cid}/{currentPage}/{requestCount}")
    public Mono<JsonResult> getCardsByPage(
            @PathVariable(required = false) Long cid,
            @PathVariable Integer currentPage,
            @PathVariable Integer requestCount
    ){
        return cardService.getCardsByPage(cid,currentPage,requestCount).collectList().map(JsonResult::ok);
    }

    @GetMapping("/page/{currentPage}/{requestCount}")
    public Mono<JsonResult> getCardsByPage(
            @PathVariable Integer currentPage,
            @PathVariable Integer requestCount
    ){
        return cardService.getCardsByPage(null,currentPage,requestCount).collectList()
                .map(JsonResult::ok);
    }
}

